package com.kaka.bus.engine.borrow.zhima;

import android.graphics.Bitmap;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.kaka.bus.R;
import com.kaka.bus.base.BaseActivity;
import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.util.logger.Logger;
import com.kaka.bus.view.MutipleLayout;

import butterknife.BindView;

/**
 * Created by zhangqi on 2017/12/21.
 * e-mail : ${email}
 * desc :
 */

public class ZhiMaActivity extends BaseActivity<ZhiMaPresenter> implements ZhiMaContract.View {

    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    @BindView(R.id.toolbar_name)
    TextView mToolbarName;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.pact_progressbar)
    ProgressBar mPactProgressbar;
    @BindView(R.id.pact_webview)
    WebView mPactWebview;
    @BindView(R.id.pact_muti)
    MutipleLayout mPactMuti;
    private String mUrl;

    @Override
    protected void initView() {
        super.initView();
        mToolbarBack.setOnClickListener(v -> finish());
        mToolbarName.setText("芝麻信用认证");
        mPactMuti.setStatus(MutipleLayout.Loading);
        initWebView();
    }

    @Override
    protected void initData() {
        mPresenter.loadData(getUserId());
    }

    @Override
    public void loadDataSuccess(HttpWrapper<String> wrapper) {
        if("222".equals(wrapper.code)){
            mPactMuti.setErrorText(wrapper.info);
            mPactMuti.setStatus(MutipleLayout.Error);
            mPactMuti.setOnReloadListener(v -> {
                showMessage("请联系客服");
            });
            showMessage(wrapper.info);
        }else if(Constant.ERROR_CODE.equals(wrapper.code)){
            mPactMuti.setErrorText(wrapper.info);
            mPactMuti.setStatus(MutipleLayout.Error);
            mPactMuti.setOnReloadListener(v -> {
                initData();
            });
            showMessage(wrapper.info);
        }else if(Constant.SUCCESS_CODE.equals(wrapper.code)){
           mPresenter.router(this);
        }else if("202".equals(wrapper.code)){
            /**
             * 需要去授权
             */
            mUrl = wrapper.getData();
            initDataUrl(mUrl);
            mPactMuti.setStatus(MutipleLayout.Success);
        }else {
            showMessage(wrapper.getInfo());
        }
    }

    private void initDataUrl(String url) {
        Logger.i(url);
        mPactWebview.loadUrl(url);
    }

    private void initWebView() {
        WebSettings settings = mPactWebview.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDefaultTextEncodingName("UTF-8");
        settings.setSupportZoom(false);
        settings.setPluginState(WebSettings.PluginState.ON);
        mPactWebview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

        mPactWebview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100) {
                    mPactProgressbar.setVisibility(View.GONE);
                } else {
                    mPactProgressbar.setVisibility(View.VISIBLE);
                    mPactProgressbar.setProgress(newProgress);
                }
            }
        });

        mPactWebview.setWebViewClient(new WebViewClient() {
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                Logger.i("onPageFinished"+url);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                if(url.contains("risk-web")){

                }
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.contains("risk-web")) {
                    initData();
                    return true;
                } else {
                    return false;
                }
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mPactWebview.canGoBack()) {

            mPactWebview.goBack(); // goBack()表示返回WebView的上一页面
            return true;
        }
        return super.onKeyDown(keyCode,event);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_pact;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }


}
